<template>
  <div class="type-nav">
    <p>{{currentIndex}}</p>
    <div class="container">
      <!-- 大导航 -->
      <h2 class="all" @mouseenter="changeshow()">全部商品分类</h2>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
      <!-- 下面导航 -->
      <transition name="sort">
        <!-- <div class="sort" @mouseleave="leaveIndex()" v-show="show"> -->
        <div class="sort" @mouseleave="leaveIndex()" @click="goSearch($event)" v-show="show">
          <div class="all-sort-list2">
            <div class="all-sort-list2">
              <div class="item bo" v-for="(c1,index) in categoryList" :key="c1.categoryId">
                <!-- @mouseenter="" -->
                <!-- 当定点设备（通常指鼠标）移动到元素上时就会触发 mouseenter 事件（就是hover） -->
                <!-- 用来保存index索引，然后
            用来添加class  -->
                <!-- v-bind:class="{'selected': item.Members}" 当names下的Members==true时给div的class动态添加selected -->
                <!-- nav标签 -->

                <!-- 当定点设备（通常指鼠标）移动到元素上时就会触发 mouseenter 事件,不会冒泡 -->

                <h3 @mouseenter="changeIndex(index,$event)" :class="{'ho':index===currentIndex}">
                  <!-- 一级分类 -->
                  <a :data-categoryName="c1.categoryName" :data-categoryId1="c1.categoryId">{{c1.categoryName}}--{{index}}</a>

                </h3>

                <div class="item-list clearfix" :class="{'show':index===currentIndex}">
                  <div class="subitem" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                    <dl class="fore">
                      <dt>
                        <!-- 二级分类 -->
                        <a :data-categoryName="c2.categoryName" :data-categoryId2="c2.categoryId">{{c2.categoryName}}</a>
                      </dt>
                      <dd>
                        <!-- <router-link to="/search/:"></router-link> -->
                        <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                          <!-- 三级分类 -->
                          <a :data-categoryName="c3.categoryName" :data-categoryId3="c3.categoryId">{{c3.categoryName}}</a>
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import _ from 'lodash'

export default {
  name: 'TypeNav',
  data () {
    return {
      currentIndex: -1,
      // 该组件在router-view,所以每次切换页面都会销毁重建,可以用生命周期函数
      show: true
    }
  },
  mounted () {
    this.typeShow()
  },
  methods: {
    changeIndex: _.debounce(function (index) {
      this.currentIndex = index
      // 快速掠过有些不触发
      // console.log(this)这个组件
      console.log(this.currentIndex)
    }, 200),
    leaveIndex () {
      this.currentIndex = -1
      if (this.$route.path !== '/home' && this.currentIndex === -1) {
        this.show = false
      }
    },
    goSearch (event) {
      const element = event.target
      const data = element.dataset
      // console.log(data)
      const { categoryname, categoryid1, categoryid2, categoryid3 } = data
      if (categoryname) {
        const location = { path: 'search' }
        const query = { categoryName: categoryname }
        if (categoryid1) {
          query.category1Id = categoryid1
        }
        if (categoryid2) {
          query.category2Id = categoryid2
        } else {
          query.category3Id = categoryid3
        }
        // location.params = this.$route.params
        location.query = query
        this.$router.push(location)
      }
    },
    // 在搜索页的逻辑
    typeShow () {
      if (this.$route.path === '/home') {
        this.show = true
      } else {
        this.show = false
      }
    },
    // 进入全部分类显示下面列表
    changeshow () {
      this.show = true
    }
  },
  computed: {
    ...mapState({
      // state是大仓库
      categoryList: state => state.home.categoryList
    })
  }
}
</script>

<style lang="less" scoped>
.ho {
  background-color: #333;
  //  display: block;
}
.item-list {
  display: block;
}
.ho1 {
  display: block;
}
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          // &:hover {
          //   background-color: #e1251b;
          // }

          h3 {
            line-height: 28px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
              text-decoration: none;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
          .show {
            display: block;
          }
        }
      }
    }
    .sort-enter {
      height: 0px;
      // transform: rotate(0deg);
    }
    .sort-enter-active {
      transition: all 0.5s;
      overflow: hidden;
    }
    .sort-enter-to {
      height: 461px;
      // transform: rotate(360deg);
    }
  }
}
</style>
